//
//  CommodityTVCell.swift
//  iPhone
//
//  Created by Toj on 8/12/22.
//

import UIKit

//protocol CommodityMobileCellDelegate: NSObjectProtocol {
//    func commodityDetailCellDidTapBuy(_ view: CommodityDetailCell)
//}

//private let buyButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class CommodityTVCell: UICollectionViewCell {
    
    var productModel: MemberProductDto? { didSet {
        guard let product = productModel else { return }
        
        if !product.isLoad {
            product.isLoad = true
            DataStatistics.sendEvent(event: DSEvent(category: st_className, action: "dataplan_product_show", label: product.name, value: "1", lt: .event))
        }
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    private lazy var backgroundImageView = UIImageView()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.textColor = .white
        view.font = UIFont.systemFont(ofSize: 24.f_scale)
        return view
    }()
    
    private lazy var tipsLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textColor = .white
        view.font = UIFont.systemFont(ofSize: 10.f_scale)
        return view
    }()
}

// MARK: - 初始化
extension CommodityTVCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(tipsLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        layoutIfNeeded()
        
        backgroundImageView.image = "commodity_all_tv".to_image
        let name = "membership_TV".localized()
        let tips = "membership_ALL_TV".localized()
        tipsLabel.text = tips
        nameLabel.text = name
        nameLabel.sizeToFit()
        
        // 32 左右间距
        let maxWidth = frame.width - 32
        var tipsHeight = tips.st_stringHeight(font: tipsLabel.font, maxWidth: maxWidth)
        let nameHeight = nameLabel.frame.height
        // 文本高度 2: 文字间的间距
        let totalHeight = tipsHeight + nameHeight + 2
        
        // 20 最低上下间距
        let margin: CGFloat = 10
        var marginY = (frame.height - totalHeight) * 0.5
        
        if margin > marginY {
            marginY = margin
            tipsHeight = frame.height - margin * 2 - nameHeight
        }
        
        nameLabel.snp.remakeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(contentView.snp.right).offset(-16)
            make.top.equalTo(marginY)
        }
        
        tipsLabel.snp.remakeConstraints { make in
            make.right.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(2)
            make.height.equalTo(tipsHeight)
        }
    }
}
